﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;

namespace Alfa.Common.ValueConverters {
	/// <summary>
	/// Value converter that translates <see cref="TimeSpan"/> to <see cref="DateTime"/>.
	/// </summary>
	public sealed class TimeSpanToDateTimeConverter : IValueConverter {
		/// <summary>
		/// Converts <see cref="TimeSpan"/> value to string.
		/// </summary>
		/// <param name="value">Value to convert. Should be <see cref="TimeSpan"/>.</param>
		/// <param name="targetType"></param>
		/// <param name="parameter"></param>
		/// <param name="culture"></param>
		/// <returns>Converted value (<see cref="DateTime"/>)</returns>
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
			if (value is TimeSpan) {
				return default(DateTime).AddTicks(((TimeSpan)value).Ticks);
			}
			return default(DateTime);
		}

		/// <summary>
		/// Converts back string value to <see cref="TimeSpan"/>.
		/// </summary>
		/// <param name="value">Value to convert. Should be <see cref="DateTime"/>.</param>
		/// <param name="targetType"></param>
		/// <param name="parameter"></param>
		/// <param name="culture"></param>
		/// <returns>Converted value (TimeSpan)</returns>
		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
			if (value is DateTime) {
				return ((DateTime)value).TimeOfDay;
			}
			return default(TimeSpan);
		}
	}
}
